﻿using ClusterAwareAdmin;
using ClusterAwareProperties;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace ClusterAwareAdminTest
{


    /// <summary>
    ///This is a test class for ResourceAdminTest and is intended
    ///to contain all ResourceAdminTest Unit Tests
    ///</summary>
    [TestClass]
    public class ResourceAdminTest
    {
        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext { get; set; }


        private static ClusterManagerAdmin _manager;


        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        [ClassInitialize]
        public static void ClassTestInitialize(TestContext testContext)
        {
            var managerInit = new ClusterManagerAdmin
            {
                Username = SharedConsts.User,
                Password = SharedConsts.Pwd,
                Domain = SharedConsts.Domain,
                Machine = SharedConsts.Machine,
                CacheInstances = false
            };
            managerInit.Connect();
            if (!managerInit.Groups.ExistGroup(SharedConsts.NewGroupTest))
            {
                managerInit.Groups.Create(SharedConsts.NewGroupTest);
            }
            if (!managerInit.Groups.ExistGroup(SharedConsts.GroupTest))
            {
                managerInit.Groups.Create(SharedConsts.GroupTest);
            }
            if (managerInit.OperationSystem == WindowsOperationSystem.Windows2008 || managerInit.OperationSystem == WindowsOperationSystem.Windows2008R2)
            {
                var node = managerInit.Groups.GetActiveNode(SharedConsts.GroupTest).Name;
                managerInit.Groups.SetPreferredOwners(SharedConsts.GroupTest, new[] { node });
            }
            if (!managerInit.Resources.ExistResource(SharedConsts.ResourceDep))
            {
                managerInit.Resources.Create(SharedConsts.GroupTest, SharedConsts.ResourceDep, ResourceTypeNamesConstants.ResourcetypeGenericApplication, false);
                var res = managerInit.Resources.GetResource(SharedConsts.ResourceDep);
                res.PrivateProperties.SetPropertyValue("CommandLine", "Notepad.exe");
                res.PrivateProperties.SetPropertyValue("CurrentDirectory", "C:\\");
                res.PrivateProperties.SetPropertyValue("InteractWithDesktop", true);
                managerInit.Resources.Save(res);
            }
            if (!managerInit.Resources.ExistResource(SharedConsts.ResourceTest))
            {
                managerInit.Resources.Create(SharedConsts.GroupTest, SharedConsts.ResourceTest, ResourceTypeNamesConstants.ResourcetypeGenericApplication, false);
                var res = managerInit.Resources.GetResource(SharedConsts.ResourceTest);
                res.PrivateProperties.SetPropertyValue("CommandLine", "Notepad.exe");
                res.PrivateProperties.SetPropertyValue("CurrentDirectory", "C:\\");
                res.PrivateProperties.SetPropertyValue("InteractWithDesktop", true);
                managerInit.Resources.Save(res);
                managerInit.Resources.AddDependency(SharedConsts.ResourceTest, SharedConsts.ResourceDep);
            }
            managerInit.Dispose();
        }
        //
        //Use ClassCleanup to run code after all tests in a class have run
        [ClassCleanup]
        public static void ClassTestCleanup()
        {
            var managerInit = new ClusterManagerAdmin
            {
                Username = SharedConsts.User,
                Password = SharedConsts.Pwd,
                Domain = SharedConsts.Domain,
                Machine = SharedConsts.Machine,
                CacheInstances = false
            };
            managerInit.Connect();
            if (managerInit.Resources.ExistResource(SharedConsts.ResourceDep))
            {
                managerInit.Resources.Delete(SharedConsts.ResourceDep);
            }
            if (managerInit.Resources.ExistResource(SharedConsts.ResourceTest))
            {
                managerInit.Resources.Delete(SharedConsts.ResourceTest);
            }
            if (managerInit.Groups.ExistGroup(SharedConsts.NewGroupTest))
            {
                managerInit.Groups.Delete(SharedConsts.NewGroupTest);
            }
            if (managerInit.Groups.ExistGroup(SharedConsts.GroupTest))
            {
                managerInit.Groups.Delete(SharedConsts.GroupTest);
            }
            managerInit.Dispose();
        }
        //
        //Use TestInitialize to run code before running each test
        [TestInitialize]
        public void Initialize()
        {
            _manager = new ClusterManagerAdmin
            {
                Username = SharedConsts.User,
                Password = SharedConsts.Pwd,
                Domain = SharedConsts.Domain,
                Machine = SharedConsts.Machine,
                CacheInstances = false
            };
            _manager.Connect();
        }
        //
        //Use TestCleanup to run code after each test has run
        [TestCleanup]
        public void Cleanup()
        {
            _manager.Dispose();
        }
        //
        #endregion

        /// <summary>
        ///A test for SetDependencies
        ///</summary>
        [TestMethod]
        public void SetDependenciesTest()
        {
            if (_manager.OperationSystem == WindowsOperationSystem.Windows2003 || _manager.OperationSystem == WindowsOperationSystem.Windows2003R2)
            {
                Assert.Inconclusive("Windows 2003 not suport Method SetDependencies");
            }
            _manager.CacheInstances = true;
            var target = new ResourceAdmin(_manager);
            var exp = target.GetDependencieseExpression(SharedConsts.ResourceTest);
            target.SetDependencies(SharedConsts.ResourceTest, "");
            var actual = target.GetDependencieseExpression(SharedConsts.ResourceTest);
            Assert.AreEqual("", actual);
            target.SetDependencies(SharedConsts.ResourceTest, exp);
            actual = target.GetDependencieseExpression(SharedConsts.ResourceTest);
            Assert.AreNotEqual("", actual);
            _manager.CacheInstances = false;
            exp = target.GetDependencieseExpression(SharedConsts.ResourceTest);
            target.SetDependencies(SharedConsts.ResourceTest, "");
            actual = target.GetDependencieseExpression(SharedConsts.ResourceTest);
            Assert.AreEqual("", actual);
            target.SetDependencies(SharedConsts.ResourceTest, exp);
            actual = target.GetDependencieseExpression(SharedConsts.ResourceTest);
            Assert.AreNotEqual("", actual);
        }

        /// <summary>
        ///A test for Rename
        ///</summary>
        [TestMethod]
        public void RenameTest()
        {
            _manager.CacheInstances = true;
            var target = new ResourceAdmin(_manager);
            target.Rename(SharedConsts.ResourceTest, SharedConsts.ResourceTest + "Rename");
            var actual = target.ExistResource(SharedConsts.ResourceTest + "Rename");
            Assert.AreEqual(true, actual);
            actual = target.ExistResource(SharedConsts.ResourceTest);
            Assert.AreEqual(false, actual);
            target.Rename(SharedConsts.ResourceTest + "Rename", SharedConsts.ResourceTest);
            actual = target.ExistResource(SharedConsts.ResourceTest);
            Assert.AreEqual(true, actual);
            actual = target.ExistResource(SharedConsts.ResourceTest + "Rename");
            Assert.AreEqual(false, actual);
            _manager.CacheInstances = false;
            target.Rename(SharedConsts.ResourceTest, SharedConsts.ResourceTest + "Rename");
             actual = target.ExistResource(SharedConsts.ResourceTest + "Rename");
            Assert.AreEqual(true, actual);
            actual = target.ExistResource(SharedConsts.ResourceTest);
            Assert.AreEqual(false, actual);
            target.Rename(SharedConsts.ResourceTest + "Rename", SharedConsts.ResourceTest);
            actual = target.ExistResource(SharedConsts.ResourceTest);
            Assert.AreEqual(true, actual);
            actual = target.ExistResource(SharedConsts.ResourceTest + "Rename");
            Assert.AreEqual(false, actual);
        }

        /// <summary>
        ///A test for RemoveRegistryCheckpoint
        ///</summary>
        [TestMethod]
        public void RemoveRegistryCheckpointTest()
        {
            Assert.Inconclusive("A method RemoveRegistryCheckpoint implemented but not tested. Wait develeper communit to test.");
        }

        /// <summary>
        ///A test for RemovePossibleOwner
        ///</summary>
        [TestMethod]
        public void AddRemovePossibleOwnerTest()
        {
            if (_manager.OperationSystem == WindowsOperationSystem.Windows2003 || _manager.OperationSystem == WindowsOperationSystem.Windows2003R2)
            {
                Assert.Inconclusive("Windows 2003 not suport Method Add/Remove PossibleOwner");
            }
            _manager.CacheInstances = true;
            var target = new ResourceAdmin(_manager);
            foreach (var node in _manager.Nodes.GetNodes())
            {
                target.RemovePossibleOwner(SharedConsts.ResourceTest,node.Name);
            }
            var ow = target.GetPossibleOwnerNode(SharedConsts.ResourceTest);
            Assert.AreEqual(0,ow.Length);
            foreach (var node in _manager.Nodes.GetNodes())
            {
                target.AddPossibleOwner(SharedConsts.ResourceTest,node.Name);
            }
            ow = target.GetPossibleOwnerNode(SharedConsts.ResourceTest);
            Assert.AreNotEqual(0,ow.Length);
             _manager.CacheInstances = false;
            foreach (var node in _manager.Nodes.GetNodes())
            {
                target.RemovePossibleOwner(SharedConsts.ResourceTest,node.Name);
            }
            ow = target.GetPossibleOwnerNode(SharedConsts.ResourceTest);
            Assert.AreEqual(0,ow.Length);
            foreach (var node in _manager.Nodes.GetNodes())
            {
                target.AddPossibleOwner(SharedConsts.ResourceTest,node.Name);
            }
            ow = target.GetPossibleOwnerNode(SharedConsts.ResourceTest);
            Assert.AreNotEqual(0,ow.Length);
       }

        /// <summary>
        ///A test for RemoveDependency
        ///</summary>
        [TestMethod]
        public void AddRemoveDependencyTest()
        {
            ClassTestInitialize(TestContext);
            _manager.CacheInstances = true;
            var target = new ResourceAdmin(_manager);
            target.RemoveDependency(SharedConsts.ResourceTest,SharedConsts.ResourceDep);
            var ow = target.GetDependenciesResource(SharedConsts.ResourceTest);
            Assert.AreEqual(0,ow.Length);
            target.AddDependency(SharedConsts.ResourceTest,SharedConsts.ResourceDep);
            ow = target.GetDependenciesResource(SharedConsts.ResourceTest);
            Assert.AreNotEqual(0,ow.Length);
        }

        /// <summary>
        ///A test for RemoveCryptoCheckpoint
        ///</summary>
        [TestMethod]
        public void RemoveCryptoCheckpointTest()
        {
            Assert.Inconclusive("A method RemoveCryptoCheckpoint implemented but not tested. Wait develeper communit to test.");
        }

        /// <summary>
        ///A test for Delete
        ///</summary>
        [TestMethod]
        public void CreateDeleteResouceTest()
        {
            ClassTestInitialize(TestContext);
            _manager.CacheInstances = true;
            var target = new ResourceAdmin(_manager);
            target.Delete(SharedConsts.ResourceTest);
            var actual = target.ExistResource(SharedConsts.ResourceTest);
            Assert.AreEqual(false,actual);
            target.Create(SharedConsts.GroupTest, SharedConsts.ResourceTest, ResourceTypeNamesConstants.ResourcetypeGenericApplication,false);
            actual = target.ExistResource(SharedConsts.ResourceTest);
            Assert.AreEqual(true, actual);
            target.Delete(SharedConsts.ResourceTest);
            actual = target.ExistResource(SharedConsts.ResourceTest);
            Assert.AreEqual(false, actual);
            ClassTestInitialize(TestContext);
            _manager.CacheInstances = false;
            target.Delete(SharedConsts.ResourceTest);
            actual = target.ExistResource(SharedConsts.ResourceTest);
            Assert.AreEqual(false, actual);
            target.Create(SharedConsts.GroupTest, SharedConsts.ResourceTest, ResourceTypeNamesConstants.ResourcetypeGenericApplication, false);
            actual = target.ExistResource(SharedConsts.ResourceTest);
            Assert.AreEqual(true, actual);
            target.Delete(SharedConsts.ResourceTest);
            actual = target.ExistResource(SharedConsts.ResourceTest);
            Assert.AreEqual(false, actual);
            ClassTestInitialize(TestContext);
        }

        /// <summary>
        ///A test for AddRegistryCheckpoint
        ///</summary>
        [TestMethod]
        public void AddRegistryCheckpointTest()
        {
            Assert.Inconclusive("A method AddRegistryCheckpoint implemented but not tested. Wait develeper communit to test.");
        }

        /// <summary>
        ///A test for AddCryptoCheckpoint
        ///</summary>
        [TestMethod]
        public void AddCryptoCheckpointTest()
        {
            Assert.Inconclusive("A method AddCryptoCheckpoint implemented but not tested. Wait develeper communit to test.");
        }

        /// <summary>
        ///A test for ResourceAdmin Constructor
        ///</summary>
        [TestMethod]
        public void ResourceAdminConstructorTest()
        {
           new ResourceAdmin(_manager);
        }
    }
}
